{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\hilar\Box\lights_2022\replication package\code\merge_admin1.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}22 Dec 2025, 18:45:58
{txt}
{com}. timer on 1
{txt}
{com}. 
. * reads in data on dams by admin1 level (state)
. * data from ArcGIS "spatial join" of GRanD to GADM level 1 
. * target is admin1 and join feature are dams
. * use "contains" and "one-to-many" and "keep all target features"
. import delimited "../data/dam_admin1_join.csv", clear
{res}{txt}(encoding automatically selected: UTF-8)
{text}(72 vars, 9,508 obs)

{com}. 
. **Generate dam counts
. 
. *first, gen separate vars for diff dam types
. *will =0 for for missing uses
. gen irrig=(use_irr=="Main" | use_irr=="Major")
{txt}
{com}. gen hydro=(use_elec=="Main" | use_elec=="Major")
{txt}
{com}. gen supply=(use_supp=="Main" | use_supp=="Major")
{txt}
{com}. gen flood=(use_fcon=="Main" | use_fcon=="Major")
{txt}
{com}. 
. 
. *clean/prep grand variables for collapsing by means/sums
. *grand missing numerics are coded to -99
. replace cap_mcm=. if cap_mcm<0
{txt}(8 real changes made, 8 to missing)

{com}. replace cap_rep=. if cap_rep<0
{txt}(540 real changes made, 540 to missing)

{com}. replace area_skm=. if area_skm<0
{txt}(20 real changes made, 20 to missing)

{com}. replace dor_pc=. if dor_pc<0
{txt}(8 real changes made, 8 to missing)

{com}. replace year=. if year<0
{txt}(408 real changes made, 408 to missing)

{com}. replace dam_hgt_m=. if dam_hgt_m<0
{txt}(472 real changes made, 472 to missing)

{com}. *6.88% of dams (472) missing height, only 8 dams missing cap_mcm 
. 
. *only dams with capacity>=.1km3 are necessarily included in GRanD 
. * variable to restrict attention to these
. * capacity in million m3 
. gen largedam=(cap_mcm>=100) & !missing(cap_mcm)
{txt}
{com}. gen largeirsuhy=largedam*(irrig | supply | hydro)
{txt}
{com}. gen irsuhy=(irrig | supply | hydro)
{txt}
{com}. 
. *reservoir capacity for only hydro dams
. gen cap_mcm_hydro=cap_mcm*hydro
{txt}(2,658 missing values generated)

{com}. 
. ** identifiers for country
. rename target_fid admin1
{res}{txt}
{com}. rename gid_1 admin1id
{res}{txt}
{com}. rename gid_0 countryid
{res}{txt}
{com}. rename shape_area admin1area
{res}{txt}
{com}. 
. label var admin1 "Admin1 # (corresponds to grid)"
{txt}
{com}. label var admin1id "Admin1 code"
{txt}
{com}. 
. #delimit ;
{txt}delimiter now ;
{com}. collapse (sum) count_irr=irrig count_flood=flood count_supply=supply count_hydro=hydro 
> count_large=largedam count_largeirsuhy=largeirsuhy count_irsuhy=irsuhy rescap=cap_mcm rescaphydro=cap_mcm_hydro resarea=area_skm height=dam_hgt_m (count) count_dams=grand_id , by(admin1 admin1id countryid admin1area) ;
{res}{txt}
{com}. #delimit cr 
{txt}delimiter now cr
{com}. 
. 
. gen ifdam=count_dams>0
{txt}
{com}. gen ifhydro=count_hydro>0
{txt}
{com}. 
. drop if missing(admin1)
{txt}(0 observations deleted)

{com}. 
. sort admin1
{txt}
{com}. 
. tempfile dams_admin1
{txt}
{com}. save `dams_admin1', replace
{txt}{p 0 4 2}
(file {bf}
C:\Users\hilar\AppData\Local\Temp\ST_4590_000001.tmp{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
C:\Users\hilar\AppData\Local\Temp\ST_4590_000001.tmp{rm}
saved
as .dta format
{p_end}

{com}. 
. 
. /*
> *need population density, not at pfaf4 level as elsewhere, so read in Gridded Population of the World a
> *source is https://data.earthdata.nasa.gov/nasa-earth/human-dimensions/sedac-root/downloads/data/gpw-v4/gpw-v4-population-density-rev11/gpw-v4-population-density-rev11_2000_30_sec_asc.zip
> */
. cd "../data"
{res}C:\Users\hilar\Box\lights_2022\replication package\data
{txt}
{com}. ras2dta, file(gpw_pop_density_2000) genxcoord(x) genycoord(y) 
{err}you must start with an empty dataset
{txt}{search r(18), local:r(18);}

end of do-file

{search r(18), local:r(18);}

{com}. do "C:\Users\hilar\Box\lights_2022\replication package\code\merge_admin1.do"
{txt}
{com}. /* 
> Data prep for state-level obs estimates
> (1) read in dams at admin1 level
> (2) starts with data at .05 degree level and aggregate to .5 degree to merge to coarser grid data
> (3) aggregrate .5 degree grid data to administrative level 1
> 
>  follows:
> * read_lights_raster.do (remote sensed droughts and lights), 
> * read_ascii_cru.do (reads CRU sc-PDSI, temp and precip)
> * read_other_rasters.do (to read in cross-sectional rasters) 
> * read_pfaf4char.do (creates pfaf4_char.dta) --- dams and other pfaf4 characteristics
> 
> preceeds reg_nocell.do
> */
. capture log close
{smcl}
{com}{sf}{ul off}